/**
 *  在constructor上写了一段比较有意思的注解，它的作用是，webpack在编译文件的时候会为该类自动添加需要注入的东西，需要注入的就是constructor的参数。
 *  如果不添加这段注解我们需要手动添加依赖注入申明 UserController.$inject = ['UserService', '$uibModal'];  添加这段注解就是为了省去写这段代码。
 */
class UserController {
    /* @ngInject*/
    constructor(UserService, $uibModal, $document, $rootScope, $http, $state, $timeout) {
        var tis = this;
        tis.message = 'hello';
        tis.isShow = false;
        tis.selected = [];
        tis.user = {};

        //page
        tis.currentPageNo = 1;
        tis.maxSize = 5;
        tis.itemPerPage = 5;

        UserService.getUsers().then(res => {
            tis.users = res.data;
            tis.totalNum = tis.users.length;

        });
        tis.addUser = () => {
            UserService.addUser(tis.user).then(result => {
                if(result.statusText == 'OK'){
                    tis.isShow = true;
                    tis.message = '保存成功！';
                    tis.users.push(result.data.data[0]);
                    tis.totalNum = tis.users.length;
                    tis.user = {};
                    $timeout(() => {
                        $state.go('user.empty');
                    }, 1000);
                }else{
                    tis.isShow = true;
                    tis.message = '保存失败！';
                }
            });
        }
        tis.cancelAddUser = () => {
            this.user={};
            $state.go('user.empty');
        }
        tis.remove = (id) => {
            UserService.remove([id]).then((data) => {
                tis.isShow = true;
                if(data.statusText == 'OK'){
                    tis.message = '删除成功！';
                    tis.users = tis.users.filter(ele => {
                        if (ele.id != id) return true;
                    });
                }else{
                    tis.message = '删除失败！';
                }
            });
        }
        tis.goAdd = () => {
            $state.go('add');
        }

        tis.removeUsers = () => {

            UserService.remove(tis.selected).then((data) => {
                this.isShow = true;
                if(data.statusText == 'OK'){
                    tis.message = '删除成功！';
                    tis.users = tis.users.filter(ele => {
                        if (tis.selected.indexOf(ele.id) < 0) return true;
                    });
                }else{
                    tis.message = '删除失败！';
                }
            });

        }

        tis.modify = (id) => {

        }

        tis.setSelected = (id) => {
            if (tis.selected.indexOf(id) >= 0) {
                let index = tis.selected.indexOf(id);
                tis.selected.splice(index, 1);
                console.log(tis.selected);
            } else {
                tis.selected.push(id);
                console.log(tis.selected);
            }
        }

        tis.selectAll = () => {
            if (tis.selected.length == tis.users.length) {
                tis.selected = [];
                tis.users.forEach(user => {
                    user.isSelected = false;
                })
            } else {
                tis.selected = [];
                tis.users.forEach(user => {
                    tis.selected.push(user.id);
                    user.isSelected = true;
                });
            }
        }

        //this.items = ['item1', 'item2', 'item3'];

        tis.open = (size, parentSelector) => {
            let parentElem = parentSelector ? angualr.element($document[0].querySelector('.mode-demo' + parentSelector)) : undefined;
            $rootScope.modalInstance = $uibModal.open({
                animation: true,
                ariaLabelledBy: 'modal-title',
                ariaDescribedBy: 'modal-body',
                templateUrl: 'myModalContent.html',
                controllerAs: 'tis',
                controller: ($scope) => {
                    $scope.items = ['item1', 'item2', 'item3'];
                    $scope.selected = {
                        item: $scope.items[0]
                    };
                    //$scope.ok = $rootScope.modalInstance.close;
                    $scope.save = function () {
                        $rootScope.modalInstance.close($scope.selected.item);
                        $http({
                            method: 'post',
                            url: 'user/save',
                            data: $scope.user
                        }).then(result => {
                            if(result.statusText == 'OK'){
                                tis.isShow = true;
                                tis.message = '保存成功！';
                                tis.users.push(result.data.data[0]);
                                //tis.openMultipleModals('保存成功！');
                            }else{
                                tis.isShow = true;
                                tis.message = '保存失败！';
                                //tis.openMultipleModals('保存失败！');
                            }
                        });
                    };

                    $scope.cancel = function () {
                        $rootScope.modalInstance.dismiss('cancel');
                    };
                    $scope.user = {};
                },
                size: size,
                appendTo: parentElem

            });
        }

        /*tis.openMultipleModals = (msg) => {
            $uibModal.open({
                animation: true,
                ariaLabelledBy: 'modal-title-bottom',
                ariaDescribedBy: 'modal-body-bottom',
                templateUrl: 'stackedModal.html',
                size: 'sm',
                controller: function($scope) {
                    $scope.msg = msg;
                    console.log('*****************************8');
                    console.log(msg);
                    console.log('*****************************8');
                }
            });
        };*/


    }
}
//UserController.$inject = ['UserService', '$uibModal'];
export default UserController;